package com.hxm.meizi.ui.activity;

import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.blankj.utilcode.utils.LogUtils;
import com.blankj.utilcode.utils.ToastUtils;
import com.hxm.meizi.R;
import com.hxm.meizi.app.Constants;
import com.hxm.meizi.entity.ApiClass;
import com.hxm.meizi.http.HttpRequest;
import com.hxm.meizi.ui.fragment.ImageFragment;

import java.util.ArrayList;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;
    private long firstTime = 0;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        registNetStatusReceiver();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

        setSupportActionBar(toolbar);
        toolbar.setTitle(R.string.app_name);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);

        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.container, ImageFragment.newInstance(Constants.BASE_URL));
        transaction.commit();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        }
        if (fragmentManager.getBackStackEntryCount() == 0) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 2000) {
                ToastUtils.showShortToast("再次点击退出程序");
                firstTime = secondTime;
            } else {
                finish();
            }
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
//            startActivity(new Intent(this, UploadActivity.class));
            HttpRequest.getInstance().getClassNames(null)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<ArrayList<ApiClass>>() {
                        @Override
                        public void call(ArrayList<ApiClass> apiClasses) {
                            LogUtils.i("api_data", apiClasses.toString());
                        }
                    });
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        transaction = fragmentManager.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        switch (id) {
            case R.id.nav_home:
                replaceFragment(transaction, Constants.BASE_URL);
                break;
            case R.id.nav_xinggan:
                replaceFragment(transaction, Constants.XING_GAN);
                break;
            case R.id.nav_japan:
                replaceFragment(transaction, Constants.JAPAN);
                break;
            case R.id.nav_taiwan:
                replaceFragment(transaction, Constants.TAIWAN);
                break;
            case R.id.nav_qingchun:
                replaceFragment(transaction, Constants.QING_CHUN);
                break;
            case R.id.nav_zipai:
                replaceFragment(transaction, Constants.ZI_PAI);
                break;
            case R.id.nav_hot:
                replaceFragment(transaction, Constants.HOT);
                break;
            case R.id.nav_tuijian:
                replaceFragment(transaction, Constants.BEST);
                break;
            case R.id.nav_zhuanti:
                replaceFragment(transaction, Constants.ZHUAN_TI);
                break;
            case R.id.nav_everyday:
                replaceFragment(transaction, Constants.ALL);
                break;
            case R.id.api_image:

                break;
        }
        transaction.commit();
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    /**
     * replace fragment
     *
     * @param transaction
     * @param url
     */
    private void replaceFragment(FragmentTransaction transaction, String url) {
        ImageFragment fragment = ImageFragment.newInstance(url);
        transaction.replace(R.id.container, fragment);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}
